package lt.marsrutai.mobile.android.adapter;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.model.ArrivalTime;
import lt.marsrutai.mobile.android.model.enums.TransportType;
import lt.marsrutai.mobile.android.util.HourMinuteFormat;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class NextArrivalTimeAdapter extends ListViewAdapter<ArrivalTime> {

    private String scopeFolder;
    private static final int MAX_ONTIME_LATE = 2; // When late by more than this, will be red
    private static final int MAX_ONTIME_EARLY = 1; // When early by more than this, will be red
    private static final int MAX_REMAINING_MINUTES = 10; // When this or less left, report minutes
    
    public NextArrivalTimeAdapter(Context context, String scopeFolder) {
        super(context, R.layout.nearest_routes_item);
        
        this.scopeFolder = scopeFolder;
    }

    @Override
    public View doGetView(int position, View view, ViewGroup parent) {

        ArrivalTime o = getItem(position);

        if (o != null) {
            TextView scheduleName = (TextView) view.findViewById(R.id.schedule_name);
            TextView routeName = (TextView) view.findViewById(R.id.route_name);
            TextView scheduledTime = (TextView) view.findViewById(R.id.scheduled_time);
            TextView expectedTime = (TextView) view.findViewById(R.id.expected_time);

            if (scheduleName != null && !TransportType.TRAIN.equals(o.getTransportType())) {
                scheduleName.setText(o.getScheduleName());
                scheduleName.setBackgroundColor(AppContext.getTransportInfoResolver().resolveTransportColor(scopeFolder, o.getTransportId()));
            }
            if (routeName != null) {
                routeName.setText(o.getDirectionName());
            }
            if (scheduledTime != null) {
                scheduledTime.setText(HourMinuteFormat.format(o.getScheduledTime()));
            }
            if (expectedTime != null) {
                
                
                long delayMinutes = Math.round((o.getExpectedTime().getTimeInMillis() - o.getScheduledTime().getTimeInMillis())/60000.0);
                long remainingMinutes = Math.round((o.getExpectedTime().getTimeInMillis() - System.currentTimeMillis())/60000.0);
                
                if (!o.getExpectedTime().equals(o.getScheduledTime()) && remainingMinutes >= 0) {           
                    // Real time                    
                    expectedTime.setVisibility(View.VISIBLE);
                    
                    String arrivalText = (remainingMinutes > MAX_REMAINING_MINUTES) ? 
                            String.format(getContext().getResources().getString(R.string.arrivals_expected_time), HourMinuteFormat.format(o.getExpectedTime())) : 
                            String.format(getContext().getResources().getString(R.string.arrivals_expected_minutes), remainingMinutes);
                    String delayText = (delayMinutes > MAX_ONTIME_LATE) ? 
                            getContext().getResources().getString(R.string.arrivals_late) : ((delayMinutes < -MAX_ONTIME_EARLY) ? 
                            getContext().getResources().getString(R.string.arrivals_early) : 
                            "");
                    String fullText = (delayText.length() > 0) ? (delayText + ", " + arrivalText) : arrivalText;
                    
                    expectedTime.setText(fullText);                     
                    expectedTime.setTextColor(getContext().getResources().getColor(delayText.length() > 0 ? R.color.red : R.color.green));            
                }              
                else if (remainingMinutes >= 0 && remainingMinutes <= MAX_REMAINING_MINUTES) {
                    // Scheduled time
                    expectedTime.setVisibility(View.VISIBLE);
                    String arrivalText = String.format(getContext().getResources().getString(R.string.arrivals_scheduled_minutes), remainingMinutes);
                    expectedTime.setText(arrivalText);                                                      
                }
                else {                                        
                    expectedTime.setVisibility(View.GONE);
                }
            }
      
        }
        return view;
    }

}
